package com.niodata.dp.expression.function.date;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class DateFormatFunction extends AbstractFunction {

  @Override
  public String getName() {
    return "date.format";
  }

  public AviatorObject call(Map<String, Object> env, AviatorObject src, AviatorObject format) {
    String fmt = FunctionUtils.getStringValue(format, env);
    Date date = (Date) src.getValue(env);
    SimpleDateFormat sdf = new SimpleDateFormat(fmt);
    String value = sdf.format(date);
    return FunctionUtils.wrapReturn(value);
  }
}
